package edu.wfu.preparedDishes.collection.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.wfu.preparedDishes.collection.entity.CollectionEntity;
import edu.wfu.preparedDishes.collection.service.CollectionService;
import edu.wfu.preparedDishes.commodity.entity.commodityVo;
import edu.wfu.preparedDishes.commodity.service.CommoditySpuService;
import edu.wfu.preparedDishes.common.utils.PageUtils;
import edu.wfu.preparedDishes.common.utils.R;
import edu.wfu.preparedDishes.recipes.entity.RecipesEntity;
import edu.wfu.preparedDishes.recipes.service.RecipesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * 收藏表;
 *
 * @author hero_miracle
 * @date 2023-11-24 19:09:31
 */
@RestController
@RequestMapping("commodity/collection")
public class CollectionController {
    @Resource
    private CollectionService collectionService;

    @Resource
    private CommoditySpuService commoditySpuService;

    @Resource
    private RecipesService recipesService;


    /**
     * 菜谱id列表
     */
    @RequestMapping("/recipeList")
    public List<RecipesEntity> getRecipeIds(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        QueryWrapper<CollectionEntity> collectionEntityQueryWrapper = new QueryWrapper<>();
        collectionEntityQueryWrapper.eq("user_id", userId) //先写死
                .eq("collect_type", 1);

        // 查询数据库
        List<CollectionEntity> collectionEntities = collectionService.list(collectionEntityQueryWrapper);

        // 将查询结果中的 recipeId 转换为 List<String>
        List<String> recipeIds = collectionEntities.stream()
                .map(CollectionEntity::getRecipesId)
                .collect(Collectors.toList());

        return this.recipeList(recipeIds);
    }


    /**
     * 菜谱收藏列表 数据经过处理再进行返回
     */
    public List<RecipesEntity> recipeList(List<String> recipeIds) {
        List<RecipesEntity> recipesEntities = new ArrayList<>();
        for (String recipeId : recipeIds) {
            RecipesEntity byId = recipesService.getById(recipeId);
            recipesEntities.add(byId);
        }
        return recipesEntities;
    }


    /**
     * 列表 数据经过处理再进行返回  辅助下个方法
     */
    @RequestMapping("/commodityList")
    public List<commodityVo> getSpuIds(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        QueryWrapper<CollectionEntity> collectionEntityQueryWrapper = new QueryWrapper<>();
        collectionEntityQueryWrapper.eq("user_id", userId);  //先写死

        // 查询数据库
        List<CollectionEntity> collectionEntities = collectionService.list(collectionEntityQueryWrapper);

        // 将查询结果中的 spuId 转换为 List<String>
        List<String> spuIds = collectionEntities.stream()
                .map(CollectionEntity::getSpuId)
                .collect(Collectors.toList());

        return this.commodityList(spuIds);
    }

    /**
     * 商品收藏列表 数据经过处理再进行返回
     */
    public List<commodityVo> commodityList(List<String> spuIds) {
        List<commodityVo> commodityVos = new ArrayList<>();
        for (String spuId : spuIds) {
            List<commodityVo> commodityVo = commoditySpuService.queryCommodities(1, 0, "",
                    "", "", "", spuId);
            commodityVos.addAll(commodityVo);
        }

        return commodityVos;
    }


    /**
     * 查询单个信息
     */
    @RequestMapping("/info/{collectId}")
    public R info(@PathVariable("collectId") String collectId) {
        CollectionEntity collection = collectionService.getById(collectId);

        return R.ok().put("collection", collection);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CollectionEntity collection, HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        //userId = "1151d924279ba1691a5e37c1017fe354";//先将用户ID进行写死
        collection.setUserId(userId);
        collectionService.save(collection);
        return R.ok(collection.getCollectId());
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CollectionEntity collection) {
        collectionService.updateById(collection);

        return R.ok();
    }

    /**
     * 多删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody List<String> collectIds) {
        collectionService.removeByIds(collectIds);
        return R.ok();
    }

    /**
     * 单删除
     */
    @DeleteMapping("/delete/{collectId}")
    public R deleteOne(@PathVariable("collectId") String collectId) {
        collectionService.removeById(collectId);
        return R.ok();
    }

}
